package cn.jcet.mapreduce.group;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @author 鲜磊
 * @Date 2020/12/14 17:53
 */
public class OrderBean implements WritableComparable<OrderBean> {

    private int order_id;

    private double price;

    public OrderBean(){
        super();
    }

    public OrderBean(int order_id, double price) {
        this.order_id = order_id;
        this.price = price;
    }

    @Override
    public int compareTo(OrderBean bean) {
        /**
         * 对订单编号进行升序，对价格进行降序
         */
        int result = 0;

        if (order_id > bean.getOrder_id()){
            result = 1 ;
        }else if (order_id < bean.getOrder_id()){
            result = -1;
        }else {
            // 对价格进行排序
            if (price > bean.getPrice()){
                result = -1;
            }else if (price < bean.getPrice()){
                result = 1;
            }
        }
        return result;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(order_id);
        dataOutput.writeDouble(price);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        order_id = dataInput.readInt();
        price = dataInput.readDouble();
    }

    public int getOrder_id() {
        return order_id;
    }

    public void setOrder_id(int order_id) {
        this.order_id = order_id;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return order_id +  "\t" + price ;
    }

}
